package com.song.controller;

import com.song.pojo.Clazz;
import com.song.pojo.ClazzQueryParam;
import com.song.pojo.Result;
import com.song.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    //班级列表分页查询
    @GetMapping
    public Result getClazzs(ClazzQueryParam param){
        log.info("分页查询班级信息，参数：{}", param);
        return Result.success(clazzService.getClazzs(param));
    }
    //根据班级id删除班级信息
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id){
        log.info("删除班级信息，参数：{}", id);
       Integer res =  clazzService.deleteClazz(id);
       if(res !=0){
           return Result.success();
       }else {
           return Result.error("删除失败");
       }

    }
    //添加班级信息
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("添加班级信息，参数：{}", clazz);
        return clazzService.addClazz(clazz) !=0 ? Result.success() : Result.error("添加失败");
    }
    //根据id查询班级信息
    @GetMapping("/{id}")
    public Result getClazz(@PathVariable Integer id){
        log.info("根据id查询班级信息，参数：{}", id);
        return Result.success(clazzService.getClazz(id));
    }
    //修改班级信息
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        log.info("修改班级信息，参数：{}", clazz);
        return clazzService.updateClazz(clazz) !=0 ? Result.success() : Result.error("修改失败");
    }
    //查询所有班级信息
    @GetMapping("/list")
    public Result getAllClazz(){
        log.info("查询所有班级信息");
        return Result.success(clazzService.getAllClazz());
    }
}
